package com.woniu.venue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.exception.coachEnter.CoachEnterException;
import com.woniu.exception.coachEnter.CoachEnterExceptionCode;
import com.woniu.exception.venueClassroom.VenueClassroomException;
import com.woniu.exception.venueClassroom.VenueClassroomExceptionCode;
import com.woniu.exception.venueInfo.VenueInfoException;
import com.woniu.exception.venueInfo.VenueInfoExceptionCode;
import com.woniu.model.Result;
import com.woniu.util.StateUtil;
import com.woniu.venue.dto.coachEnter.CoachEnterDto;
import com.woniu.venue.dto.venueClassroom.VenueClassroomDto;
import com.woniu.venue.dto.venueInfo.VenueInfoDto;
import com.woniu.venue.model.CoachEnter;
import com.woniu.venue.model.VenueInfo;
import com.woniu.venue.dao.VenueInfoDao;
import com.woniu.venue.param.venueInfo.AddVenueInfoParam;
import com.woniu.venue.param.venueInfo.VenueInfoLoadParam;
import com.woniu.venue.service.VenueInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@Service
public class VenueInfoServiceImpl extends ServiceImpl<VenueInfoDao, VenueInfo> implements VenueInfoService {


    @Autowired
    private VenueInfoDao venueInfoDao;

    @Override
    public PageInfo<VenueInfoDto> load(VenueInfoLoadParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        param.setPorp(ObjectUtil.isEmpty(param.getPorp())?"score":param.getPorp());
        param.setOrder(ObjectUtil.isEmpty(param.getOrder())?"DESC":param.getOrder());
        ArrayList<VenueInfoDto> venueInfoDtos = venueInfoDao.load(param);
        PageInfo<VenueInfoDto> venueInfoDtoPageInfo = new PageInfo<>(venueInfoDtos);
        return venueInfoDtoPageInfo;
    }


    @Override
    public void changeState(Integer venueInfoId, Integer state) {
        VenueInfo venueInfo = venueInfoDao.selectById(venueInfoId);
        if (ObjectUtil.isEmpty(venueInfo)) {
            throw new VenueInfoException(VenueInfoExceptionCode.VENUE_INFO_NOT_EXIST);
        }
        if (venueInfo.getVenueState() == state || venueInfo.getVenueState() == StateUtil.VENUE_INFO_STATE_NOT_BAN) {
            throw new VenueInfoException(VenueInfoExceptionCode.VENUE_INFO_STATE_ERROR);
        }
        venueInfo.setVenueState(state);
        venueInfoDao.updateById(venueInfo);
    }

    @Override
    public VenueInfoDto addVenueInfo(AddVenueInfoParam param) {
        QueryWrapper<VenueInfo> qw = new QueryWrapper<VenueInfo>().eq("venue_id", param.getVenueId());
        VenueInfo venueInfo = venueInfoDao.selectOne(qw);
        if (ObjectUtil.isNotEmpty(venueInfo))
            throw new VenueInfoException(VenueInfoExceptionCode.VENUE_INFO_HAD_EXIST);
        VenueInfo venueInfo1 = BeanUtil.toBean(param, VenueInfo.class);
        venueInfo1.setCreateTime(DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd hh:mm:ss"));
        venueInfo1.setVenueImages(JSONUtil.toJsonStr(param.getVenueImages()));
        venueInfo1.setVenueLicenses(JSONUtil.toJsonStr(param.getVenueLicenses()));
        venueInfoDao.insert(venueInfo1);
        VenueInfoDto venueInfoDto = BeanUtil.toBean(venueInfo1, VenueInfoDto.class);
        return venueInfoDto;
    }
}
